/*
 * @Description: 
 * @version: 
 * @Author: XiaoQiYan
 * @Date: 2021-08-24 10:42:49
 * @LastEditors: XiaoQiYan
 * @LastEditTime: 2021-08-24 11:00:28
 */
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class CalendarDemo {
    public static void main(String[] args) {
        System.out.println("完整显示日期时间:");
        DateFormat fdate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String str = fdate.format(new Date());
        System.out.println(str);

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());

        System.out.println("年: "+calendar.get(Calendar.YEAR));
        System.out.println("月: " + calendar.get(Calendar.MONTH));
        System.out.println("分钟: " + calendar.get(Calendar.MINUTE));
        System.out.println("今天是今年的第 "+calendar.get(Calendar.DAY_OF_YEAR)+"天");
        System.out.println("今天是本月的第 " + calendar.get(Calendar.DAY_OF_MONTH)+"天");
        calendar.add(Calendar.HOUR_OF_DAY,3);
        System.out.println("三小时后的时间: "+calendar.getTime());

        str = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SS")).format(calendar.getTime());
        System.out.println(str);

        calendar.setTime(new Date());
        str = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SS")).format(calendar.getTime());
        System.out.println(str);

        Calendar calendarNew = Calendar.getInstance();

        calendarNew.add(Calendar.HOUR,-5);
        System.out.println("时间比较: "+calendarNew.compareTo(calendar));

        calendarNew.add(Calendar.HOUR, 7);
        System.out.println("时间比较: " + calendarNew.compareTo(calendar));

        calendarNew.add(Calendar.HOUR, -2);
        System.out.println("时间比较: " + calendarNew.compareTo(calendar));
    
        System.out.println((new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SS")).format(calendarNew.getTime()));
        System.out.println((new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SS")).format(calendar.getTime()));
        System.out.println("时间比较: " + calendarNew.compareTo(calendar));
    }
}
